<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="form">
      <el-form-item
        label="旧密码："
        prop="password"
        style="margin-bottom: 10px"
      >
        <el-input
          v-model="ruleForm.password"
          placeholder="请输入旧密码"
          type="password"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item
        label="新密码"
        prop="newPassword"
        style="margin-bottom: 10px"
      >
        <el-input
          v-model="ruleForm.newPassword"
          placeholder="请输入新密码"
          type="password"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item label="重复新密码" prop="againNewPassword">
        <el-input
          v-model="ruleForm.againNewPassword"
          placeholder="请再次输入新密码"
          type="password"
          show-password
          @keyup.enter.native="submitForm('ruleForm')"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
          style="
            width: 100px;
            border: 1px solid $mainColor !important;
            background-color: $mainColor;
            color: #fff;
          "
          >确定修改</el-button
        >
        <el-button @click="resetForm('ruleForm')" style="width: 100px"
          >重 置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import BASE64 from "@/utils/base64.js";
import { clearLoginInfo } from "@/utils/cleanLogin";
export default {
  inject: ["reload"],
  data() {
    var validOldPassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入旧密码"));
      } else if (value.length < 8) {
        callback(new Error("请输入正确的旧密码"));
      } else {
        callback();
      }
    };
    var validNewPassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else if (value.length < 10) {
        callback(new Error("请输入不小于10位的密码"));
      } else if (
        !/^(?![A-Za-z0-9]+$)(?![a-z0-9\W]+$)(?![A-Za-z\W]+$)(?![A-Z0-9\W]+$)[a-zA-Z0-9\W]{10,}$/.test(
          value
        )
      ) {
        callback(new Error("密码必须包含大小写字母、数字和特殊符号"));
      } else {
        if (this.ruleForm.againNewPassword !== "") {
          this.$refs.ruleForm.validateField("againNewPassword");
        }
        callback();
      }
    };
    var validNewPassword2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.newPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        password: "",
        newPassword: "",
        againNewPassword: "",
      },
      rules: {
        // password: [{ validator: validOldPassword, trigger: "blur" }],
        // newPassword: [{ validator: validNewPassword, trigger: "blur" }],
        againNewPassword: [{ validator: validNewPassword2, trigger: "blur" }],
      },
    };
  },
  methods: {
    // 修改密码
    update() {
      this.$put(`${this.$Url.account.newPassword}`, this.ruleForm).then(
        (data) => {
          console.log(data);
          this.$message({
            type: "success",
            message: "修改成功!",
          });
          let that = this;
          setTimeout(function () {
            // 修改密码后强制登出
            clearLoginInfo();
            that.$emit("hideDialog", true);
            that.$nextTick(() => {
              that.$router.replace({
                name: "Home",
              });
            });
          }, 1000);
        }
      );
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.update();
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.form {
  padding: 0 15px 20px;
}
</style>
